import runtime, {
  async
} from '../../lib/runtime/runtime'
import request from '../../request/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageList: [],
    isCollect: false,
    cart_num: 0,
  },
  cartList: {}, //购物车数据

  //获取数据
  async getMessageList(goods_id) {
    let res = await request('/goods/detail', {
      params: {
        goods_id: goods_id
      }
    })
    console.log(res)
    this.cartList = res
    //判断商品是否被收藏
    let collect = wx.getStorageSync("collect") || []
    //遍历数组，有一项符合则返回true,剩余的元素不会再执行检测
    let isCollect = collect.some(x => x.goods_id === res.goods_id)
    //获取购物车数量
    let cart_num = wx.getStorageSync("cart_num") || 0
    this.setData({
      messageList: res,
      isCollect,
      cart_num
    })
  },
  //图片预览
  showBigPic(e) {
    //商品图片
    const urls = this.data.messageList.pics.map(v => v.pics_mid);
    const url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls
    })
  },
  //收藏商品
  handleCollect() {
    let isCollect = false
    //获取缓存中收藏的商品
    let collect = wx.getStorageSync("collect") || [];
    console.log(collect);
    //判断商品是否被收藏过
    let index = collect.findIndex(x => x.goods_id === this.data.messageList.goods_id)
    console.log(index);
    //index不为-1,已收藏
    if (index !== -1) {
      // 已收藏，则取消收藏
      collect.splice(index, 1)
      isCollect = false
      wx.showToast({
        title: '取消收藏',
        icon: 'success',
        mask: true
      })
    } else {
      //没收藏，则收藏
      collect.push(this.data.messageList)
      isCollect = true
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
    }
    //添加到缓存
    wx.setStorageSync('collect', collect);
    //修改isCollect属性
    this.setData({
      isCollect
    })
  },
  //加入购物车
  handleAddCart() {
    //获取本地储存数据
    let cart = wx.getStorageSync('cart') || []
    //判断是否添加过该商品
    let index = cart.findIndex(x => x.goods_id === this.cartList.goods_id)
    //-1不存在，添加该商品
    if (index === -1) {
      this.cartList.num = 1
      this.cartList.checked = true
      cart.push(this.cartList)
    } else {
      //已经添加过，数量加一
      cart[index].num++
    }
    //获取购物车每项商品数量，在进行相加得到总数
    let sum = cart.map(x => x.num).reduce((prev, cur) => {
      return prev + cur;
    });
    wx.setStorageSync('cart_num', sum)
    wx.setStorageSync('cart', cart)
    //弹窗
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      mask: true
    })
    this.setData({
      cart_num: sum
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {
      gid
    } = options
    console.log(gid);
    this.getMessageList(gid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})